package com.returnsoft.security.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.security.dto.PersonDto;
import com.returnsoft.security.eao.PersonEao;
import com.returnsoft.security.entity.Person;
import com.returnsoft.security.exception.ConversionExcepcion;
import com.returnsoft.security.exception.EaoExcepcion;
import com.returnsoft.security.exception.SecurityExcepcion;
import com.returnsoft.security.util.Conversion;

/**
 * Session Bean implementation class PersonBean
 */
@Stateless
@LocalBean
public class PersonBean implements PersonInterface ,Serializable{
	private static final long serialVersionUID = 1L;
	@EJB
	private PersonEao personEao;
	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public PersonBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<PersonDto> findPersons(List<Integer> departmentsId,
			List<Integer> subDepartmentsId, String documentNumber, String firstname, String lastname)
			throws SecurityExcepcion {
		try {

			List<Person> persons = personEao.findPersons(departmentsId,
					subDepartmentsId, documentNumber,firstname,lastname);
			List<PersonDto> personsDto = new ArrayList<PersonDto>();
			for (Person person : persons) {
				PersonDto pd = conv.fromPerson(person);
				personsDto.add(pd);
			}

			return personsDto;
		} catch (EaoExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityExcepcion(e.getMessage());
		}
	}

	@Override
	public PersonDto add(PersonDto newPerson) throws SecurityExcepcion {
		try {
			Person createdPerson = conv.toPerson(newPerson);
			createdPerson = personEao.add(createdPerson);
			PersonDto createdPersonDto = conv.fromPerson(createdPerson);

			return createdPersonDto;

		} catch (EaoExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityExcepcion(e.getMessage());
		}
	}

	@Override
	public PersonDto findById(Integer personId) throws SecurityExcepcion {
		try {
			Person person = personEao.findPerson(personId);
			PersonDto pd = conv.fromPerson(person);
			return pd;
		} catch (EaoExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityExcepcion(e.getMessage());
		}
	}

	@Override
	public PersonDto findByDocumentNumber(String documentNumber)
			throws SecurityExcepcion {
		try {
			Person person = personEao.findByDocumentNumber(documentNumber);
			PersonDto pd = conv.fromPerson(person);
			return pd;
		} catch (EaoExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityExcepcion(e.getMessage());
		}
	}

}
